package keeto.com.vn.bms.ui.activity;

import java.util.ArrayList;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.config.IBMSConfig;
import keeto.com.vn.bms.model.BMSUserModel;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class LoginBMSActivity extends Activity implements OnClickListener {
	String TAG = LoginBMSActivity.class.getName();

	private CheckBox ckbSavePass;
	private EditText edtUser, edtPass;
	private Boolean savePass;
	private Button btnLogin;
	private String userName;
	private String Password;
	private BMSSharedPreferences preferences;
	private ArrayList<BMSUserModel> arrayList;
	private LogonListenner mListenner = new LogonListenner();



	public static void actionLaunch(Context context) {
		Intent intent = new Intent(context, LoginBMSActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_bms_layout);
		preferences = new BMSSharedPreferences(LoginBMSActivity.this);
		initUIComponent();

	}

	private void initUIComponent() {
		btnLogin = (Button) findViewById(R.id.btn_primary_login);
		btnLogin.setOnClickListener(this);
		edtUser = (EditText) findViewById(R.id.edt_username);
		edtPass = (EditText) findViewById(R.id.edt_password);
		ckbSavePass = (CheckBox) findViewById(R.id.ckb_save_pass);
		ckbSavePass.setChecked(false);

		if (preferences.getRememberCb(this)){
			ckbSavePass.setChecked(true);
			edtUser.setText(preferences.getUserLoginName(this));
			edtPass.setText(preferences.getUserLoginPassword(this));
		}
		btnLogin.setText(this.getString(R.string.btn_primary_login));

	}



	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_primary_login:
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
			testCheckLogin();
			break;
		default:
			break;
		}
	}

	private void testCheckLogin(){
		userName = edtUser.getText().toString();
		Password = edtPass.getText().toString();
		if (userName.equals("") && (Password.equals(""))){
			Toast.makeText(LoginBMSActivity.this, this.getResources().getString(R.string.error_login), Toast.LENGTH_SHORT).show();
		}else if (Password.equals("") && !userName.equals("")){
			Toast.makeText(LoginBMSActivity.this, this.getResources().getString(R.string.error_password), Toast.LENGTH_SHORT).show();
		}else if (!Password.equals("") && userName.equals("")){
			Toast.makeText(LoginBMSActivity.this, this.getResources().getString(R.string.error_username), Toast.LENGTH_SHORT).show();
		}else{
			BMSCore.getInstance(this).taskLogon(userName, Password, mListenner);

		}
	}

	class LogonListenner extends BMSListener{

		@Override
		public void finishLogon(final int returnCode, final BMSUserModel userModel) {
			// TODO Auto-generated method stub

			super.finishLogon(returnCode, userModel);
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					if (returnCode == IBMSProtocol.STATUS_CODE_OK){
						if(ckbSavePass.isChecked()){
							preferences.setUserLoginName(userName);
							preferences.setUserLoginPassword(Password);
							preferences.setRememberCb(true);
						}else{
							preferences.setUserLoginName("");
							preferences.setUserLoginPassword("");
							preferences.setRememberCb(false);
						}
						preferences.setUserAuthKey(userModel.getAuthKey());
						preferences.setCaseGroup(userModel.getCaseGroupName());
						preferences.setTypeKeyUser(Integer.parseInt(userModel.getGroupId()));
						preferences.setUserPermission(Integer.parseInt(userModel.getGroupId()));
						preferences.setUserID(userModel.getUserID());
						preferences.setUserFullName(userModel.getFullName());
						preferences.setGroupID(userModel.getGroupId());
						
						switch (preferences.getUserPermission(LoginBMSActivity.this)) {
						case IBMSConfig.KEY_TYPE_LEADERSHIP:
						case IBMSConfig.KEY_TYPE_ADMIN:
						case IBMSConfig.KEY_TYPE_DOCTOR:
							BMSChatActivity.actionLaunch(LoginBMSActivity.this);
							break;
						case IBMSConfig.KEY_TYPE_TECHNICIAL:
						case IBMSConfig.KEY_TYPE_MARKETING:
						case IBMSConfig.KEY_TYPE_CUSTOMER:
							HomeBMSActivity.actionLaunch(LoginBMSActivity.this);
							break;
						default:
							break;
						}
						LoginBMSActivity.this.finish();
					}else if (returnCode == IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
						Toast.makeText(LoginBMSActivity.this, LoginBMSActivity.this.getResources().getString(R.string.error_login_user), Toast.LENGTH_SHORT).show();
					}else if (returnCode == IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(LoginBMSActivity.this, LoginBMSActivity.this.getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}
				}
			});
		}

	}


}
